package com.hot100.easy;

/**
 * @ClassName Demo22
 * @Description 二叉树直径 求左子树和右子树最大深度之和
 * @Author zt
 * @Date 14:38 2022/6/16
 * @Version 1.0
 **/
public class Demo22 {

    int max;
    public static void main(String[] args) {

    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


        public int diameterOfBinaryTree(TreeNode root) {

             return  dep(root);

        }

    private int dep(TreeNode root) {
        if(root==null){
            return 0;
        }

        if(root.left==null||root.right==null){
            return 1;
        }
        int left=dep(root.left);
        int right=dep(root.right);
        max=Math.max(max,left+right);
        return Math.max(right,left)+1;
    }

}